package cn.dida.controller;

import cn.dida.exception.BizzException;
import cn.dida.model.HttpResponseBodyWrapper;
import cn.dida.model.LgsDepot;
import cn.dida.model.Pageable;
import cn.dida.model.UserSession;
import cn.dida.service.LgsDepotService;
import cn.dida.util.ValidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by zhangzhiling on 2017/2/8.
 */
@Controller
@RequestMapping("/lgsDepot")
public class LgsDepotController extends BaseController {

    @Autowired
    private LgsDepotService lgsDepotService;

    /**
     * 获取地点列表
     * @param request
     * @param keyword 地点名称
     * @param sta  状态 1可用，0屏蔽
     * @param page
     * @return
     * @throws Exception
     */
    @GetMapping("/list")
    public ModelAndView getDepotList(HttpServletRequest request, String keyword, Integer sta, Integer page) throws Exception{

        Map<String, Object> model = new HashMap<String, Object>();
        UserSession userSession = getUserSession(request);

        page = (page == null || page < 1) ? 1 : page;
        keyword = StringUtils.trimToNull(keyword);
        Integer companyId = userSession.getCompanyId();
        Pageable<LgsDepot> list = lgsDepotService.getDepotList(companyId,keyword,sta,page);

        model.put("depotList", list);
        model.put("keyword", keyword);
        model.put("sta", sta);
        return new ModelAndView("lgs_depot").addAllObjects(model);
    }

    /**
     * 提交新建地点
     * @param request
     * @param lgsDepot
     * @return
     */
    @ResponseBody
    @PostMapping ("/postLgsDepot")
    public HttpResponseBodyWrapper setDepot(HttpServletRequest request, @RequestBody LgsDepot lgsDepot){

        lgsDepot.setName(StringUtils.trimToNull(lgsDepot.getName()));
        lgsDepot.setAddress(StringUtils.trimToNull(lgsDepot.getAddress()));
        lgsDepot.setSta(lgsDepot.getSta() == null ? 1 : lgsDepot.getSta());

        if(lgsDepot.getName() == null){
            throw new BizzException(BizzException.error_code_invalid_params, "请输入地点名称！");
        }
        if (!Pattern.matches(ValidUtil.WENZI_PATTERN,lgsDepot.getName())) {
            throw new BizzException(BizzException.error_code_invalid_params, "地点名称仅限汉字、数字、字母！");
        }
        if(lgsDepot.getAddress() == null){
            throw new BizzException(BizzException.error_code_invalid_params, "请输入详细地址！");
        }

        UserSession userSession = getUserSession(request);
        Integer companyId = userSession.getCompanyId();
        if(lgsDepotService.testName(companyId,lgsDepot.getName()) != null ){
            throw new BizzException(BizzException.error_code_invalid_params, "地点名称已存在！");
        }
        lgsDepot.setCompanyId(companyId);
        lgsDepot.setcAdmin(userSession.getId_admin());

        Boolean result = lgsDepotService.setDepot(lgsDepot);
        return new HttpResponseBodyWrapper("true");
    }

    /**
     * 根据id 获取地点详情
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/getLgsDepot/{id}")
    public HttpResponseBodyWrapper getLgsDepot(@PathVariable Integer id){

        Map<String, Object> map = new HashMap<>();
        LgsDepot lgsDepot = lgsDepotService.getLgsDepot(id);
        map.put("lgsDepot", lgsDepot);
        return new HttpResponseBodyWrapper(map);
    }

    /**
     * 提交修改地点
     * @param request
     * @param lgsDepot
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public HttpResponseBodyWrapper update(HttpServletRequest request, @RequestBody LgsDepot lgsDepot){

        lgsDepot.setName(StringUtils.trimToNull(lgsDepot.getName()));
        lgsDepot.setAddress(StringUtils.trimToNull(lgsDepot.getAddress()));

        if(lgsDepot.getId() == null){
            throw new BizzException(BizzException.error_code_invalid_params, "地点不存在，请重新操作");
        }
        if(lgsDepot.getAddress() == null){
            throw new BizzException(BizzException.error_code_invalid_params, "请输入详细地址");
        }
        if(lgsDepot.getName() == null){
            throw new BizzException(BizzException.error_code_invalid_params, "请输入地点名称");
        }
        if (!Pattern.matches(ValidUtil.WENZI_PATTERN,lgsDepot.getName())) {
            throw new BizzException(BizzException.error_code_invalid_params, "地点名称仅限汉字、数字、字母！");
        }

        UserSession userSession = getUserSession(request);
        Integer companyId = userSession.getCompanyId();
        LgsDepot depot = lgsDepotService.testName(companyId,lgsDepot.getName());
        if( depot != null && depot.getId() !=lgsDepot.getId()){
            throw new BizzException(BizzException.error_code_invalid_params, "地点名称已存在！");
        }
        Boolean result = lgsDepotService.update(lgsDepot);
        return new HttpResponseBodyWrapper("true");
    }
}
